package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Set;

import com.ruoyi.common.bean.sys.SysUserVo;
import com.ruoyi.common.core.domain.model.LoginInfo;
import com.ruoyi.common.core.domain.model.WxLoginBody;
import com.ruoyi.common.enums.LoginTypeEnum;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.sign.RsaUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.domain.BaseResponse;
import com.ruoyi.system.domain.SysMenu;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.permission.SysLoginService;
import com.ruoyi.permission.SysPermissionService;
import com.ruoyi.system.service.ISysMenuService;

/**
 * 登录验证
 * 
 * @author ruoyi
 */
@RestController
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private ISysUserService userService;

    /**
     * 登录方法
     * 
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public BaseResponse login(@RequestBody LoginBody loginBody)
    {
        String password = null;
        try {
            password = RsaUtils.decryptByPrivateKey(loginBody.getPassword());
        } catch (Exception e) {
            throw new ServiceException("无效密码");
        }

        BaseResponse ajax = BaseResponse.success();
        LoginInfo loginInfo = new LoginInfo();

        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), password, loginBody.getCode(),
                loginBody.getUuid(), LoginTypeEnum.WEB);
        loginInfo.setToken( token);

        ajax.setData(loginInfo);
        return ajax;
    }

    @PostMapping("/wxlogin")
    public BaseResponse wxlogin(@RequestBody LoginBody loginBody)
    {

        String password = loginBody.getPassword();

        BaseResponse ajax = BaseResponse.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), password, loginBody.getCode(),
                loginBody.getUuid(), LoginTypeEnum.WX);

        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setToken( token);
        ajax.setData(loginInfo);
        return ajax;
    }

    @PostMapping("/wxlogin-bak")
    public BaseResponse wxlogin2(@RequestBody WxLoginBody loginBody)
    {

        BaseResponse ajax = BaseResponse.success();
        // 生成令牌
        String token = loginService.wxLogin(loginBody);

        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setToken( token);
        ajax.setData(loginInfo);
        return ajax;
    }

    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public BaseResponse getInfo()
    {
        SysUserVo user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        BaseResponse ajax = BaseResponse.success();
        // 用户信息
        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setUser(user);
        loginInfo.setRoles(roles);
        loginInfo.setPermissions(permissions);

        ajax.setData(loginInfo);
        return ajax;
    }

    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public BaseResponse getRouters()
    {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return BaseResponse.success(menuService.buildMenus(menus));
    }
}
